package com.qzyh.esb.egsa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzyh.esb.egsa.bean.vo.MenuTreeVO;
import com.qzyh.esb.egsa.bean.vo.RoleVO;
import com.qzyh.esb.egsa.entity.Dept;
import com.qzyh.esb.egsa.entity.Menu;
import com.qzyh.esb.egsa.entity.Role;
import com.qzyh.esb.egsa.service.IRoleService;
import com.qzyh.esb.egsa.utils.FillUtil;
import com.qzyh.esb.egsa.utils.PageResultObject;
import com.qzyh.esb.egsa.utils.ResultObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author whq
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    @ApiOperation(value = "查询角色列表")
    public PageResultObject<Role> list(@ApiParam(value = "页码") @RequestParam(value="pageNum",required=false) Long pageNum,
                                        @ApiParam(value = "每页条数") @RequestParam(value="pageSize",required=false) Long pageSize,
                                        @ApiParam(value = "角色名称") @RequestParam("roleName") String roleName) {
        IPage<Role> page = new Page<>(pageNum==null?0:pageNum, pageSize==null?10:pageSize);
        page = roleService.getList(page, roleName);
        PageResultObject<Role> pageResultObject = PageResultObject.success();
        pageResultObject.setPageNum(page.getCurrent());
        pageResultObject.setPageSize(page.getSize());
        pageResultObject.setTotal(page.getTotal());
        pageResultObject.setData(page.getRecords());
        return pageResultObject;
    }

    @GetMapping("/roleAndMenu")
    @ApiOperation(value = "获取角色绑定的菜单权限集合")
    public ResultObject<List<String>> superior(@ApiParam(value = "角色id") @RequestParam("id") String id) {
        return roleService.getRoleAndMenu(id);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存角色信息")
    public ResultObject save(@RequestBody RoleVO vo) {
        logger.info("请求参数："+vo);
        return roleService.saveRoleAndMenu(vo);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据id删除角色信息")
    public ResultObject deleteById(@ApiParam(value = "角色id") @RequestParam("id") String id) {
        logger.info("删除角色信息id："+id);
        return roleService.deleteById(id);
    }

    @GetMapping("/queryByIds")
    @ApiOperation(value = "通过id查询部门信息")
    @ApiResponses(value = { @ApiResponse(code = 1, message = "系统繁忙，请稍后重试") })
    public ResultObject<List<Role>> queryByIds(@ApiParam(value = "角色id集合字符串") @RequestParam("idsStr") String idsStr) {
        List<Role> roleList = roleService.queryByIds(idsStr);
        return ResultObject.success(roleList);
    }

}
